// pages/blindCriteria/blindCriteria.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    formlist: [
      { isrequire: true, label: '单位类别', key: 'unitType', index: 0, rangeKey: 'name', valueType: 'text', tips: '请选择', type: 'select', data: [] },
      { isrequire: true, label: '年龄', key: 'age', valueType: 'number', index: [0, 0], tips: '请选择', type: 'select', mode: 'multiSelector', data: [] },
      {
        isrequire: true, label: '身高', key: 'height', index: [0, 0], valueType: 'text', tips: '请选择', type: 'select', mode: 'multiSelector', data: []
      },
      { isrequire: true, label: '学历', key: 'degree', valueType: 'text', tips: '请选择', type: 'select', data: ['不限', '博士后', '博士 ', '硕士', '本科', '专科', '高中', '高中以下'] },
      { isrequire: true, label: '月收入', key: 'monthIncome', index: [0, 0], valueType: 'text', tips: '请选择', type: 'select', mode: 'multiSelector', data: [] },
      { isrequire: true, label: '所在地', key: 'location', valueType: 'text', tips: '请选择', type: 'select', mode: 'region' },
      { isrequire: true, label: '婚姻状况', key: 'marriageStatus', valueType: 'text', tips: '请选择', type: 'select', data: ['未婚', '离异', '不限'] },
      { isrequire: true, label: '购房状况', key: 'purchaseStatus', valueType: 'text', tips: '请选择', type: 'select', data: ['无', '有', '不限'] },
      { isrequire: true, label: '购车状况', key: 'carBuyingStatus', valueType: 'text', tips: '请选择', type: 'select', data: ['无', '有', '不限'] },
      { isrequire: true, label: '属相', key: 'animalSign', valueType: 'text', tips: '请选择', type: 'select', data: ['不限', '鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'] },
      { isrequire: true, label: '星座', key: 'constellation', valueType: 'text', tips: '请选择', type: 'select', data: ['不限', '白羊座', '金牛座', '双子座', '巨蟹座', '狮子座', '处女座', '天秤座', '天蝎座', '射手座', '摩羯座', '水瓶座', '双鱼座'] },
      { isrequire: true, label: '厨艺', key: 'cooking', valueType: 'text', tips: '请输入', type: 'input' },
      { isrequire: true, label: '是否抽烟', key: 'smoking', valueType: 'text', tips: '请输入', type: 'input' },
      { isrequire: true, label: '是否喝酒', key: 'drinking', valueType: 'text', tips: '请输入', type: 'input' }
    ],
    formData: {
      height: "",
      age: '',
      degree: '',
      unitType: '',
      monthIncome: '',
      marriageStatus: '',
      purchaseStatus: '',
      carBuyingStatus: '',
      location: '',
      animalSign: '',
      constellation: '12',
      cooking: '',
      smoking: "",
      drinking: ''
    },
    showData: {
      unitType: '',
      degree: '',
      height: '',
      age: '',
      marriageStatus: '',
      purchaseStatus: '',
      carBuyingStatus: '',
      animalSign: '',
      constellation: '双鱼座',
    },
    active: '',
    canSubmit: false,
  },
  bindPickerChange: function (e) {
    var item = e.currentTarget.dataset.item;
    if (item.data) {
      if (item.data[e.detail.value].id) {
        this.data.formData[item.key] = item.data[e.detail.value].id;
        this.data.showData[item.key] = item.data[e.detail.value][item.rangeKey]
      } else {
        this.data.formData[item.key] = e.detail.value;
        this.data.showData[item.key] = item.data[e.detail.value]
      }
    } else {
      this.data.formData[item.key] = e.detail.value
    }
    this.setData({
      ['formData.' + item.key]: this.data.formData[item.key],
      ['showData.' + item.key]: this.data.showData[item.key],
    })
  },
  bindMultiPickerChange: function (e) {
    var item = e.currentTarget.dataset.item;
    var index = e.currentTarget.dataset.index;
    this.data.formlist[index].index = e.detail.value;
    // this.data.showData[item.key] = item.data[0][e.detail.value[0]].value + '-' + item.data[1][e.detail.value[1]].value;
    // this.data.formData[item.key] = item.data[0][e.detail.value[0]].id + '-' + item.data[1][e.detail.value[1]].id;
    this.data.formData[item.key] = (e.detail.value[0] == 0 ? '0' : item.data[0][e.detail.value[0]]) + '-' + (e.detail.value[1] == 0 ? '0' : item.data[1][e.detail.value[1]]);
    this.data.showData[item.key] = item.data[0][e.detail.value[0]] + '-' + item.data[1][e.detail.value[1]];
    if (e.detail.value[0] == 0 && e.detail.value[1] == 0) {
      this.data.formData[item.key] = '0';
    }
    this.setData({
      ['formData.' + item.key]: this.data.formData[item.key],
      ['showData.' + item.key]: this.data.showData[item.key],
    })
  },
  bindColumnchange: function (e) {
    var item = e.currentTarget.dataset.item;
    var index = e.currentTarget.dataset.index;
    if (e.detail.column == 0) {
      if (e.detail.value > 0) {
        // var arr = [{ id: '0', value: '不限' }];
        var arr = ['不限'];
        arr = arr.concat(item.data[0].slice(e.detail.value, item.data[0].length));
        this.setData({
          ['formlist[' + index + '].data[1]']: arr
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo'))
    })
    var that = this;
    wx.getStorage({
      key: 'criteria',
      success: function (res) {
        if (res.data) {
          for (var j in that.data.formData) {
            if (res.data[j] !== '' && res.data[j] != undefined && res.data[j] != null) {
              if (j == 'location') {
                that.data.formData[j] = res.data[j].split(',');
              } else {
                if (res.data[j] == -1) {
                  that.data.formData[j] = '';
                } else {
                  that.data.formData[j] = res.data[j];
                }
                that.data.formData[j] = res.data[j];
              }
            }
          }
        }
        that.setData({
          formData: that.data.formData
        })
        that.getUnitType();
        that.getAge();
        that.getHeight();
        that.setSelect('degree', 3);
        that.setSelect('marriageStatus', 6);
        that.setSelect('purchaseStatus', 7);
        that.setSelect('carBuyingStatus', 8);
        that.setSelect('animalSign', 9);
        that.setSelect('constellation', 10);
        that.getMonthIncome();
        wx.removeStorage({ key: 'criteria' });
      }

    })

  },
  getMonthIncome: function () {
    var monthIncomeArr = ['不限'];
    // for (var i = 1; i <= 20; i += 2) {
    for (var i = 1; i <= 20; i++) {
      monthIncomeArr.push(i * 1000);
    }
    monthIncomeArr.push('20000以上')
    if (this.data.formData.monthIncome != '') {
      if (this.data.formData.monthIncome.indexOf('-') >= 0) {
        var monthIncome = this.data.formData.monthIncome.split('-');
        var heightiIndex = [0, 0];
        monthIncomeArr.forEach(function (ele, index) {
          if (ele == monthIncome[0]) {
            heightiIndex[0] = index;
          }
          if (ele == monthIncome[1]) {
            heightiIndex[1] = index;
          }
        })
        this.setData({
          ['formlist[4].index']: heightiIndex,
          'showData.monthIncome': (monthIncome[0] == '0' ? '不限' : monthIncome[0]) + '-' + (monthIncome[1] == '0' ? '不限' : monthIncome[1])
        })
      } else {
        this.setData({
          'showData.monthIncome': '不限-不限',
        })
      }
    }
    this.setData({
      ['formlist[4].data']: [monthIncomeArr, monthIncomeArr]
    })
  },
  setSelect: function (key, index) {
    if (this.data.formData[key] !== '') {
      this.setData({
        ['formlist[' + index + '].index']: this.data.formData[key],
        ['showData.' + key]: this.data.formlist[index].data[this.data.formData[key]]
      })
    }
  },
  getHeight: function () {
    var arr = ['不限', '150', '155', '160', '165', '170', '175', '180', '185', '190', '195'];
    var that = this;
    if (that.data.formData.height != '') {
      if (that.data.formData.height.indexOf('-') >= 0) {
        var height = that.data.formData.height.split('-');
        var heightiIndex = [0, 0];
        arr.forEach(function (ele, index) {
          if (ele == height[0]) {
            heightiIndex[0] = index;
          }
          if (ele == height[1]) {
            heightiIndex[1] = index;
          }
        })
        that.setData({
          ['formlist[2].index']: heightiIndex,
          'showData.height': (height[0] == '0' ? '不限' : height[0]) + '-' + (height[1] == '0' ? '不限' : height[1])
        })
      } else {
        that.setData({
          'showData.height': '不限-不限',
        })
      }
    }
    that.setData({
      ['formlist[2].data']: [arr, arr]
    })
  },
  getAge: function () {//年龄
    var arr = ['不限'];
    for (var i = 18; i <= 50; i++) {
      arr.push(i);
    }
    if (this.data.formData.age != '') {
      if (this.data.formData.age.indexOf('-') >= 0) {
        var age = this.data.formData.age.split('-');
        this.setData({
          ['formlist[1].index']: [age[0] == '0' ? '不限' : parseInt(age[0]) - 17, age[1] == '0' ? '不限' : parseInt(age[1]) - 17],
          'showData.age': (age[0] == '0' ? '不限' : age[0]) + '-' + (age[1] == '0' ? '不限' : age[1])
        })
      } else {
        this.setData({
          'showData.age': '不限-不限',
        })
      }
    }
    this.setData({
      ['formlist[1].data']: [arr, arr]
    })
  },
  getUnitType: function () {
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGyIndustryResource/all', //仅为示例，并非真实的接口地址
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {},
      success: function (res) {
        if (res.data.success) {
          res.data.data.unshift({ id: '0', name: '不限' });
          res.data.data.forEach(function (ele, index) {
            if (that.data.formData.unitType != '') {
              if (that.data.formData.unitType == ele.id) {
                that.setData({
                  ['formlist[0].index']: index,
                  'showData.unitType': ele.name
                })
              }
            }
          })
          that.setData({
            ['formlist[0].data']: res.data.data
          })
        }
      }
    })
  },
  changeTextarea: function (e) {//输入框值改变事件
    var key = e.target.dataset.value;
    this.data.formData[key] = e.detail.value;
    this.setData({
      formData: this.data.formData
    })
  },
  saveBasicInfo: function (e) {
    var param = {};
    var flag = true;
    var form = e.detail.value;
    for (var j in this.data.formData) {
      if (form[j] != undefined) {
        param[j] = form[j];
        this.data.formData[j] = form[j];
      } else {
        param[j] = this.data.formData[j];
      }
      if (this.data.formData[j] === '') {
        flag = false;
      }
    }
    if (flag) {
      if (param.location != '') {
        param.location = param.location.join(',');
      }
      param.userId = this.data.userInfo.id;
      var that = this;
      wx.request({
        url: app.globalData.httpApiCar + 'spouseCriteria/update', //仅为示例，并非真实的接口地址
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        data: param,
        success: function (res) {
          if (res.data.success) {
            wx.navigateBack({
              delta: 1
            })
            // wx.redirectTo({
            //   url: '/pages/blindMine/blindMine',
            // })
          }
          else {
            wx.showToast({
              title: res.data.error,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '您还有信息必填项没填！',
        showCancel:false
      })
    }
  },
  saveCanel: function (e) {
    // wx.redirectTo({
    //   url: '/pages/blindMine/blindMine'
    // })
    wx.navigateBack();
  },
  changeValue() {
    var flag = true;
    var item = this.data.formData;
    if ((item.brandCar == '') || (item.plateNumber == '') || (item.carColor == '') || (item.seat == '')) {
      flag = false;
    }
    this.setData({
      canSubmit: flag
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})